package com.gitee.grower.util.germ

import com.germ.germplugin.api.dynamic.gui.GermGuiScreen
import com.germ.germplugin.api.dynamic.gui.GermGuiSlot
import com.gitee.grower.util.germ.germ.GermPartDSL
import org.bukkit.inventory.ItemStack
import taboolib.common.platform.function.info
import taboolib.library.configuration.ConfigurationSection
import taboolib.library.xseries.XMaterial

class GermSlotDSL : GermPartDSL<GermGuiSlot>() {

    var width = ""
    var height = ""
    var size = "1"
    var itemSize = "36"
    var fillPath = "36"
    var emptyPath = "36"
    var subscript : String? = null
    var hoverSound = "36"
    var clickSound = "36"
    var identity : String? = null
    var item: ItemStack? = null
    var invalid = false
    var interact = false

    override val type: String = "texture"
    var onclick = mutableMapOf<Enum<*>, (GermGuiScreen.() -> Unit)>()

    override fun infuse(config: ConfigurationSection) {
        name = config.name
        config.getString("locationX")?.let { locationX = it }
        config.getString("locationY")?.let { locationY = it }
        config.getString("locationZ")?.let { locationZ = it }
        config.getString("size")?.let { size = it }
        config.getString("width")?.let { width = it }
        config.getString("height")?.let { height = it }
        config.getString("width")?.let { width = it }
        config.getString("itemSize")?.let { itemSize = it }
        config.getString("fillPath")?.let { fillPath = it }
        config.getString("hoverSound")?.let { hoverSound = it }
        config.getString("clickSound")?.let { clickSound = it }
        config.getString("emptyPath")?.let { emptyPath = it }
        config.getString("subscript")?.let { subscript = it }

        identity = config.getString("identity")
        invalid = config.getBoolean("invalid")
        interact = config.getBoolean("interact")

        enable = config.getBoolean("enable")
    }

    override fun create(): GermGuiSlot {
        return GermGuiSlot(name).also {
            it.locationX = locationX
            it.locationY = locationY
            it.locationZ = locationZ
            it.enable = enable
            it.width = width
            it.height = height
            it.size = size
            it.fillPath = fillPath
            it.hoverSound = hoverSound
            it.clickSound = clickSound
            it.emptyPath = emptyPath
            it.isInteract = interact
            it.isInvalid = invalid
            it.itemSize = itemSize
            it.identity = identity
            it.subscript = subscript
            if (item != null) {
                it.setItemStack(this.item)
            }
            onclick.forEach { (key, value) ->
                it.registerCallbackHandler({ _, _ ->
                    value(it.germGuiScreen)
                }, key)
            }

        }
    }
}